﻿#region References
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Reflection;
using LuaInterface;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;
using Ness.Engine.GUI;
#endregion

namespace Ness.Engine.Lua
{
    /// <summary>
    /// Allows Lua to invoke the engine
    /// </summary>
    class LuaEngineProxy
    {
        #region Public variables
        public Engine game;
        public LuaParser lua;
        #endregion

        /// <summary>
        /// Initialises the engine proxy and registers functions
        /// </summary>
        /// <param name="lua">The current lua parser being used</param>
        /// <param name="_game">The current engine instance</param>
        public LuaEngineProxy(LuaParser lua, Engine _game)
        {
            this.lua = lua;
            game = _game;
        }

        #region Lua Functions
        /// <summary>
        /// Creates a key bind to be invoked through the engine
        /// </summary>
        /// <param name="key">Key char to use - EG: K</param>
        /// <param name="func">Lua Function to be registered</param>
        public void CreateKeyBind(string key, object func)
        {
            Debugger.WriteLine("Key (" + key + ") bound to " + func.ToString());
            Binds.AddBind(key, func);
        }

        /// <summary>
        /// Executes a keybind
        /// </summary>
        /// <param name="key">Key to execute</param>
        public void ExecuteKeyBind(string key)
        {
            Binds.DoBind(key);
        }

        public void CreateEventTrigger(string name, LuaTable mint, LuaTable maxt, LuaInterface.LuaFunction func)
        {
            if (game.IS_CLIENT)
            {
                Debugger.WriteError("Event triggers are server-side only!");
                return;
            }

            Vector3 min = LuaParser.tableToVector(mint);
            Vector3 max = LuaParser.tableToVector(maxt);
            Debugger.WriteLua("Registered event trigger: " + name + " at " + min + " " + max);
            game.eventTriggers.Add(new Ness.Engine.Interact.EventTrigger(name, min, max, func));
        }

        public void AddFrameListener(string name, LuaInterface.LuaFunction func)
        {
            if (game.IS_CLIENT)
            {
                Debugger.WriteError("Frame listeners are server-side only!");
                return;
            }
            game.frameListeners.Add(name, func);
        }

        public void SetConCmd(string cmd, object value)
        {
            Debugger.WriteLua("Cmd (" + cmd + ") set to (" + value + ")");
            ConCmd.SetCmdData(cmd, value);
        }

        public void ToggleNoclip()
        {
            if (game.IS_CLIENT)
            {
                game.client.SendPlayerKey("NOCLIP");
            }
            else
            {
                game.server.SendPlayerKey("NOCLIP", game.playerID);
            }
        }

        public void RemoveFrameListener(string name)
        {
            if (game.IS_CLIENT)
            {
                Debugger.WriteError("Frame listeners are server-side only!");
                return;
            }
            game.frameListeners.Remove(name);
        }

        /// <summary>
        /// Sets the background buffer's colour
        /// </summary>
        /// <param name="position"></param>
        public void SetClearBuffer(LuaInterface.LuaTable tbl)
        {
            Vector3 col = LuaParser.tableToVector(tbl);
            game.drawToScreen.ClearBuffer.ClearColour = new Color((byte)col.X, (byte)col.Y, (byte)col.Z);
        }
        #endregion


    }
}
